/*   1:    */package com.linkage.bss.commons.util;
/*   2:    */
/*   3:    */import java.io.ByteArrayInputStream;
/*   4:    */import java.io.ByteArrayOutputStream;
/*   5:    */import java.io.ObjectInputStream;
/*   6:    */import java.io.ObjectOutputStream;
/*   7:    */import java.io.PrintStream;
/*   8:    */import java.io.UnsupportedEncodingException;
/*   9:    */import java.util.Date;
/*  10:    */
/*  15:    */public class ByteArray
/*  16:    */{
/*  17:    */  private int size;
/*  18:    */  private byte[] buf;
/*  19: 19 */  private int length = 0;
/*  20:    */  
/*  21:    */  public ByteArray() {
/*  22: 22 */    this.size = 20;
/*  23: 23 */    this.buf = new byte[this.size];
/*  24:    */  }
/*  25:    */  
/*  26:    */  public ByteArray(int initSize) {
/*  27: 27 */    this.size = initSize;
/*  28: 28 */    this.buf = new byte[this.size];
/*  29:    */  }
/*  30:    */  
/*  31:    */  public void put(byte[] buffer) {
/*  32: 32 */    for (int i = 0; i < buffer.length; i++) {
/*  33: 33 */      put(buffer[i]);
/*  34:    */    }
/*  35:    */  }
/*  36:    */  
/*  37:    */  public void put(byte b) {
/*  38: 38 */    if (this.length >= this.size) {
/*  39: 39 */      this.size *= 2;
/*  40: 40 */      byte[] tmp = new byte[this.size];
/*  41: 41 */      System.arraycopy(this.buf, 0, tmp, 0, this.length);
/*  42: 42 */      this.buf = tmp;
/*  43:    */    }
/*  44: 44 */    this.buf[(this.length++)] = b;
/*  45:    */  }
/*  46:    */  
/*  47:    */  public byte get(int index) throws ArrayIndexOutOfBoundsException {
/*  48: 48 */    if (index >= this.length) {
/*  49: 49 */      throw new ArrayIndexOutOfBoundsException();
/*  50:    */    }
/*  51: 51 */    return this.buf[index];
/*  52:    */  }
/*  53:    */  
/*  54:    */  public void set(int index, byte b) throws ArrayIndexOutOfBoundsException {
/*  55: 55 */    if (index >= this.length) {
/*  56: 56 */      throw new ArrayIndexOutOfBoundsException();
/*  57:    */    }
/*  58: 58 */    this.buf[index] = b;
/*  59:    */  }
/*  60:    */  
/*  61:    */  public void clear() {
/*  62: 62 */    this.length = 0;
/*  63: 63 */    this.buf = null;
/*  64: 64 */    this.size = 0;
/*  65:    */  }
/*  66:    */  
/*  67:    */  public String getString(String charset) throws UnsupportedEncodingException {
/*  68: 68 */    return new String(this.buf, charset).trim();
/*  69:    */  }
/*  70:    */  
/*  71:    */  public String toString() {
/*  72: 72 */    return new String(this.buf, 0, this.length).trim();
/*  73:    */  }
/*  74:    */  
/*  78:    */  public byte[] getArray()
/*  79:    */  {
/*  80: 80 */    return this.buf;
/*  81:    */  }
/*  82:    */  
/*  83:    */  public int length() {
/*  84: 84 */    return this.length;
/*  85:    */  }
/*  86:    */  
/*  87:    */  public int size() {
/*  88: 88 */    return this.size;
/*  89:    */  }
/*  90:    */  
/*  91:    */  public static void main(String[] args) throws Exception {
/*  92: 92 */    ByteArray ba = new ByteArray();
/*  93: 93 */    String src = "你是个好人，我不是，38荣34343434343，其实它也不kdfjowejif是!";
/*  94: 94 */    byte[] srcBytes = src.getBytes();
/*  95: 95 */    for (int i = 0; i < srcBytes.length; i++) {
/*  96: 96 */      ba.put(srcBytes[i]);
/*  97:    */    }
/*  98: 98 */    System.out.println(ba);
/*  99: 99 */    System.out.println("length=" + ba.getString("UTF-8").getBytes().length);
/* 100:100 */    System.out.println("size=" + ba.size());
/* 101:    */    
/* 102:102 */    ByteArrayInputStream in = new ByteArrayInputStream(
/* 103:103 */      "aa总共大开发aaiiaaa".getBytes("UTF-8"));
/* 104:104 */    byte[] buffer = new byte[256];
/* 105:105 */    ByteArray byteArray = new ByteArray();
/* 106:106 */    while (in.read(buffer) >= 0) {
/* 107:107 */      System.out.println(buffer.length);
/* 108:108 */      byteArray.put(buffer);
/* 109:    */    }
/* 110:110 */    System.out.println("ba:" + byteArray.getString("UTF-8").trim());
/* 111:    */    
/* 112:112 */    test();
/* 113:    */  }
/* 114:    */  
/* 115:115 */  public static void test() throws Exception { ByteArrayOutputStream bo = new ByteArrayOutputStream();
/* 116:116 */    ObjectOutputStream oo = new ObjectOutputStream(bo);
/* 117:117 */    oo.writeObject(new Date());
/* 118:    */    
/* 119:119 */    ByteArrayInputStream in = new ByteArrayInputStream(bo.toByteArray());
/* 120:120 */    byte[] buffer = new byte[256];
/* 121:121 */    ByteArray byteArray = new ByteArray();
/* 122:    */    
/* 123:123 */    while (in.read(buffer) >= 0) {
/* 124:124 */      System.out.println(buffer.length);
/* 125:125 */      byteArray.put(buffer);
/* 126:    */    }
/* 127:    */    
/* 128:128 */    Object obj = null;
/* 129:129 */    ByteArrayInputStream bi = new ByteArrayInputStream(byteArray.getArray());
/* 130:130 */    ObjectInputStream oi = new ObjectInputStream(bi);
/* 131:131 */    obj = oi.readObject();
/* 132:132 */    Date obj2 = (Date)obj;
/* 133:133 */    System.out.println("date:" + obj2);
/* 134:    */  }
/* 135:    */}


/* Location:           E:\workspace\sungl111\workspace_promotion\BizHallConfig\WebRoot\WEB-INF\lib\bss.commons-0.1.jar
 * Qualified Name:     com.linkage.bss.commons.util.ByteArray
 * JD-Core Version:    0.7.0-SNAPSHOT-20130630
 */